package com.gable.report.service.impl;

import com.gable.report.common.util.MonthEnumUtil;
import com.gable.report.dao.IChartsForHospitalMaintainDao;
import com.gable.report.dao.IChartsForHospitalMaintenanceDao;
import com.gable.report.model.*;
import com.gable.report.service.ChartsForHospitalMaintainInfoService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.gable.report.util.ChartsUtil.getParamMap;


/**
 * Created by lvdouStart on 2017/8/16.
 * 医院报表维修情况service实现类
 */
@Service
public class ChartsForHospitalMaintainInfoServiceImpl implements ChartsForHospitalMaintainInfoService {

    private final Log logger = LogFactory.getLog(this.getClass());

    @Autowired
    IChartsForHospitalMaintainDao chartsForHospitalMaintainDao;

    @Autowired
    IChartsForHospitalMaintenanceDao chartsForHospitalMaintenanceDao;



    @Override
    public Map<String, Object> queryAssetsForOverview(Long hospitalId, ChartsHospitalQuery chartsHospitalQuery) throws Exception {
        Map<String, Object> returnMap = new HashMap<>();
        Map<String, Object> paramMap = getParamMap(hospitalId, chartsHospitalQuery);
        ChartsMaintainInfoView allAssets = chartsForHospitalMaintainDao.queryAllAssets(paramMap);
        paramMap.put("asId",1);
        paramMap.put("asName","'大维修'");
        paramMap.put("maintainSize",1);
        Map<String,Object> bigRepair = chartsForHospitalMaintainDao.queryMaintainSizeInfo(paramMap);
        paramMap.put("asId",2);
        paramMap.put("asName","'中维修'");
        paramMap.put("maintainSize",2);
        Map<String,Object> middleRepair = chartsForHospitalMaintainDao.queryMaintainSizeInfo(paramMap);
        paramMap.put("asId",3);
        paramMap.put("asName","'小维修'");
        paramMap.put("maintainSize",3);
        Map<String,Object> smallRepair = chartsForHospitalMaintainDao.queryMaintainSizeInfo(paramMap);
        returnMap.put("allAssets",allAssets);
        returnMap.put("list", Arrays.asList(bigRepair,middleRepair,smallRepair));
        return returnMap;
    }

    @Override
    public Map<String, Object> queryDepartments(Long hospitalId, ChartsHospitalQuery chartsHospitalQuery) throws Exception {
        Map<String, Object> returnMap = new HashMap<>();
        Map<String, Object> paramMap = getParamMap(hospitalId, chartsHospitalQuery);
        ChartsMaintainInfoView allAssets = chartsForHospitalMaintainDao.queryAllAssets(paramMap);
        List<ChartsMaintainInfoView> list = chartsForHospitalMaintainDao.queryDepartments(paramMap);

        //设置平均指方便气泡图四个象限设置
        Double avgPrice = 0.0;
        Double avgEstimateTotalCost = 0.00;
        if(allAssets != null && list != null && list.size() != 0){
            avgPrice = allAssets.getCurDevicePrice()*1.0/list.size();
            avgEstimateTotalCost = allAssets.getCurEstimateTotalCost()*1.0/list.size();
        }
        /*Double[] dArray = getAvgOperationRate(paramMap);
        returnMap.put("avgUpkeepRate",dArray[0]);
        returnMap.put("avgInspectionRate",dArray[1]);*/
        returnMap.put("avgDevicePrice",avgPrice);
        returnMap.put("avgEstimateTotalCost",avgEstimateTotalCost);
        returnMap.put("avgAgeLimitMeasure",allAssets.getCurAgeLimitMeasure());
        returnMap.put("avgEstimateCostWanyuan",allAssets.getCurEstimateCostWanyuan());
        returnMap.put("list",list);
        return returnMap;
    }

    @Override
    public Map<String, Object> queryDeviceModels(Long hospitalId, ChartsHospitalQuery chartsHospitalQuery) throws Exception {
        Map<String, Object> returnMap = new HashMap<>();
        Map<String, Object> paramMap = getParamMap(hospitalId, chartsHospitalQuery);
        ChartsMaintainInfoView allAssets = chartsForHospitalMaintainDao.queryAllAssets(paramMap);
        List<ChartsMaintainInfoView> list = chartsForHospitalMaintainDao.queryDeviceModels(paramMap);
        //设置平均指方便气泡图四个象限设置
        Double avgPrice = 0.0;
        Double avgEstimateTotalCost = 0.00;
        if(allAssets != null && list != null && list.size() != 0){
            avgPrice = allAssets.getCurDevicePrice()*1.0/list.size();
            avgEstimateTotalCost = allAssets.getCurEstimateTotalCost()*1.0/list.size();
        }
       /* Double[] dArray = getAvgOperationRate(paramMap);
        returnMap.put("avgUpkeepRate",dArray[0]);
        returnMap.put("avgInspectionRate",dArray[1]);*/
        returnMap.put("avgDevicePrice",avgPrice);
        returnMap.put("avgEstimateTotalCost",avgEstimateTotalCost);
        returnMap.put("avgAgeLimitMeasure",allAssets.getCurAgeLimitMeasure());
        returnMap.put("avgEstimateCostWanyuan",allAssets.getCurEstimateCostWanyuan());
        returnMap.put("list",list);
        return returnMap;
    }

    @Override
    public Map<String, Object> queryDeviceTypes(Long hospitalId, ChartsHospitalQuery chartsHospitalQuery) throws Exception {
        Map<String, Object> returnMap = new HashMap<>();
        Map<String, Object> paramMap = getParamMap(hospitalId, chartsHospitalQuery);
        ChartsMaintainInfoView allAssets = chartsForHospitalMaintainDao.queryAllAssets(paramMap);
        List<ChartsMaintainInfoView> list = chartsForHospitalMaintainDao.querySecondDeviceTypes(paramMap);
        //设置平均指方便气泡图四个象限设置
        Double avgPrice = 0.0;
        Double avgEstimateTotalCost = 0.00;
        if(allAssets != null && list != null && list.size() != 0){
            int size = 0; //二级分类数量
            for(ChartsMaintainInfoView superObj : list){
                if(!CollectionUtils.isEmpty(superObj.getList()))
                    size = size + superObj.getList().size(); //计算二级设备分类种数
            }
            avgPrice = allAssets.getCurDevicePrice()*1.0/size;
            avgEstimateTotalCost = allAssets.getCurEstimateTotalCost()*1.0/size;
        }
        /*Double[] dArray = getAvgOperationRate(paramMap);
        returnMap.put("avgUpkeepRate",dArray[0]);
        returnMap.put("avgInspectionRate",dArray[1]);*/
        returnMap.put("avgDevicePrice",avgPrice);
        returnMap.put("avgEstimateTotalCost",avgEstimateTotalCost);
        returnMap.put("avgAgeLimitMeasure",allAssets.getCurAgeLimitMeasure());
        returnMap.put("avgEstimateCostWanyuan",allAssets.getCurEstimateCostWanyuan());
        returnMap.put("list",list);
        return returnMap;
    }

    @Override
    public Map<String, Object> queryDeviceBrands(Long hospitalId, ChartsHospitalQuery chartsHospitalQuery) throws Exception {
        Map<String, Object> returnMap = new HashMap<>();
        Map<String, Object> paramMap = getParamMap(hospitalId, chartsHospitalQuery);
        List<ChartsMaintainInfoView> list = chartsForHospitalMaintainDao.queryDeviceBrands(paramMap);
        returnMap.put("list",list);
        return returnMap;
    }


    @Override
    public Map<String, Object> queryForMonth(Long hospitalId, ChartsHospitalQuery chartsHospitalQuery) throws Exception {
        Map<String, Object> returnMap = new HashMap<>();
        Map<String, Object> paramMap = getParamMap(hospitalId, chartsHospitalQuery);
        int curYear = (Integer) paramMap.get("curYear");
        paramMap.put("lastYearEnd",new StringBuffer().append("'").append(curYear).append("-1-1'").toString());
        List<ChartsMaintainInfoView> list = chartsForHospitalMaintainDao.queryForMonth(paramMap);
        for(ChartsMaintainInfoView obj: list){
            obj.setName(MonthEnumUtil.getValue(obj.getId()));
        }
        returnMap.put("list",list);
        return returnMap;
    }

    @Override
    public Map<String, Object> queryDeviceInfoByModelName(Long hospitalId, ChartsHospitalQuery chartsHospitalQuery) throws Exception {
        Map<String, Object> returnMap = new HashMap<>();
        Map<String, Object> paramMap = getParamMap(hospitalId, chartsHospitalQuery);
        List<ChartsAssetsMaintainInfoView> list = chartsForHospitalMaintainDao.queryDeviceInfoByModelName(paramMap);
        returnMap.put("list", list);
        return returnMap;
    }

    @Override
    public Map<String, Object> queryMaintainRecord(Long hospitalId, ChartsHospitalQuery chartsHospitalQuery) throws Exception {
        Map<String, Object> returnMap = new HashMap<>();
        Map<String, Object> paramMap = getParamMap(hospitalId, chartsHospitalQuery);
        List<ChartsForMaintainRecord> list = chartsForHospitalMaintainDao.queryMaintainRecord(paramMap);
        returnMap.put("list", list);
        return returnMap;
    }

    @Override
    public Map<String, Object> queryHaltRecord(Long hospitalId, ChartsHospitalQuery chartsHospitalQuery) throws Exception {
        Map<String, Object> returnMap = new HashMap<>();
        Map<String, Object> paramMap = getParamMap(hospitalId, chartsHospitalQuery);
        List<ChartsForMaintainRecord> list = chartsForHospitalMaintainDao.queryHaltRecord(paramMap);
        returnMap.put("list", list);
        return returnMap;
    }

    /**
     * @Author: 杨青桦
     * @Description: 9月7日业务修改后，暂时没用
     * @Date: 2017/8/6 13:32
     * @param paramMap
     * @return
     */
    private Map<String,Object> getForMonthParamMap(Map<String,Object> paramMap ,int month){
        StringBuffer sb = new StringBuffer();
        int curYear = (Integer) paramMap.get("curYear");
        paramMap.put("thisMonthStart",sb.append(curYear).append("-").append(month).append("-1").toString());
        sb.setLength(0);
        paramMap.put("lastYearEnd",sb.append(curYear).append("-1-1").toString());
        sb.setLength(0);
        if(month != 12){
            paramMap.put("thisMonthEnd",sb.append(curYear).append("-").append(month+1).append("-1").toString());
            sb.setLength(0);
            paramMap.put("lastMonthEnd",sb.append(curYear-1).append("-").append(month+1).append("-1").toString());
        }else{
            paramMap.put("thisMonthEnd",sb.append(curYear+1).append("-1-1").toString());
            sb.setLength(0);
            paramMap.put("lastMonthEnd",sb.append(curYear).append("-1-1").toString());
        }
        sb.setLength(0);
        paramMap.put("lastMonthStart",sb.append(curYear-1).append("-").append(month).append("-1").toString());
        return paramMap;
    }

    /** 
     * @Author: 杨青桦
     * @Description: 维护接口中获取平均保养率和巡检率 (暂时不用)
     * @Date: 2017/8/10 17:53
     * @param paramMap
     * @return
     */
    private Double[] getAvgOperationRate(Map<String,Object> paramMap){
        Double[] dArray = new Double[2];

        Map<String,Object> upkeepRate = chartsForHospitalMaintenanceDao.queryUpkeepOverview(paramMap);
        Map<String,Object> inspectionRate = chartsForHospitalMaintenanceDao.queryInspectionOverview(paramMap);
        dArray[0] = (Double)upkeepRate.get("jnUpkeepPercentage");
        dArray[1] = (Double)inspectionRate.get("jnInspectionPercentage");
        return dArray;
    }
}
